package cn.com.api.controller;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import cn.com.api.entity.ApiGoods;
import cn.com.api.entity.ApiGoods_p;
import cn.com.api.entity.ApiT_goods_p;
import cn.com.api.service.ApiDianPingService;
import cn.com.api.service.ApiGoodsService;
import cn.com.api.service.ApiShopsService;
import cn.com.api.service.ApiT_dianpingService;
import cn.com.api.service.ApiT_goods_pService;
import cn.com.api.service.ApiT_log_userService;
import cn.com.api.service.ApiT_productsService;
import cn.com.api.service.ApiTypeService;
import cn.com.api.service.CheckService;
import cn.com.api.service.StartOnLoadService;
import cn.com.api.service.SystemService;
/*import cn.com.web.service.GoodsService;*/
import cn.com.util.*;

@Controller
@RequestMapping(Utils.SYS_API + "goods/")
public class ApiGoodsController {

	private static Logger logger = Logger.getLogger(ApiGoodsController.class);

	@Autowired
	private ApiGoodsService goodService;
	@Autowired
	private ApiDianPingService apiDianPingService;
	@Autowired
	private SystemService systemService;
	@Autowired
	private ApiT_productsService apiT_productsService;
	/*
	 * @Autowired private GoodsService goodsService;
	 */
	@Autowired
	private ApiT_goods_pService goodspService;
	@Autowired
	private ApiTypeService apiTypeService;
	@Autowired
	private ApiShopsService apiShopsService;
	@Autowired
	private StartOnLoadService startOnLoadService;
	@Autowired
	private CheckService checkService;
	@Autowired
	private ApiT_log_userService apiT_log_userService;
	@Autowired
	private ApiDianPingService dianPingService;

	/**
	 * 默认查询
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@Transactional
	@RequestMapping("/getGoodsDef")
	public void getGoodsDef(HttpServletRequest request, HttpServletResponse response) throws Exception {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		List<Map<String, Object>> datas = goodService.getGoodsDef();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		map.put("datas", datas);
		out.print(JsonUtil.object2json(map));
		out.close();
	}

	/**
	 * 根据id查询/商品详情----------商品详情接口
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/getGoodsById")
	public void getGoodsById(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		String id = request.getParameter("id");
		String cityid = request.getParameter("cityid");
		try {
			Map<String, Object> datas = goodService.getGoodsByProductId(id);
			if ("1".equals(datas.get("is_ziying"))) {
				datas.put("coming", startOnLoadService.dictionaryMap().get("TRANSIT_TIME_ZI"));
			} else {
				datas.put("coming", startOnLoadService.dictionaryMap().get("TRANSIT_TIME"));
			}
			List<Map<String, Object>> products = apiT_productsService.searGoodIdAndCityId(datas.get("goods_id") + "",
					cityid);
			List<ApiGoods_p> images = goodService.findGoods_pByGoodsId(Long.valueOf(datas.get("goods_id") + ""));

			Map<String, Object> dianping = apiDianPingService
					.searchDianPingCountRateByGoodsid(Long.valueOf(datas.get("goods_id") + ""));

			// 是否显示图文详情
			String isShow = "0";
			if (datas != null && images != null) {
				if (images.size() > 0) {
					isShow = "1";
				} else {
					if (datas.size() > 0) {
						String description = datas.get("description").toString();
						if (!Util.isNull(description)) {
							isShow = "1";
						}
					}
				}
			}
			// -----更新商品的访问次数
			apiT_log_userService.updateGoodsVisit(id);
			map.put("isShow", isShow);
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put("datas", datas);
			map.put("dianping", dianping);
			map.put("products", products);
			map.put("images", images);

			map.put(Utils.MESSAGE, "查询成功！");

		} catch (Exception e) {
			logger.error(Util.getStackTraceAsString(e));
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}
		//
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}

	/**
	 * 分享商品
	 * 
	 * @param productsid
	 * @return
	 */
	@RequestMapping("/shareProductInfo")
	public void shareProductInfo(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String proid = request.getParameter("proid");// 货品id
		Map<String, Object> res = new HashMap<String, Object>();
		try {
			if (proid != null && !"".equals(proid)) {
				Map<String, Object> proInfo = new HashMap<String, Object>();
				proInfo = goodService.shareProInfo(proid);
				proInfo.put("url", "/api/goods/shareProductDetail?proid=" + proInfo.get("product_id"));
				res.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				res.put(Utils.MESSAGE, "查询成功！");
				res.put("datas", proInfo);
			}
		} catch (Exception e) {
			logger.error(e);
			logger.error(Util.getStackTraceAsString(e));
			res.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			res.put(Utils.MESSAGE, "系统异常！");
		}
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(res));
		out.close();
	}

	/**
	 * 分享商品信息
	 * 
	 * @param productsid
	 * @return
	 */
	@RequestMapping("/shareProductDetail")
	public ModelAndView shareProductDetail(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView("forward:/goodDetail/sharePage/sharePage.jsp");
		String proid = request.getParameter("proid");// 货品id
		Map<String, Object> data = new HashMap<String, Object>();
		data = goodService.shareProDetail(proid);
		mv.addObject("info", data);
		return mv;
	}

	/**
	 * 条件查询/商品列表-------用户端 查询
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/getGoodsByType")
	public void getGoodsByType(HttpServletRequest request, HttpServletResponse response) throws IOException {
		Map<String, Object> map = new HashMap<String, Object>();
		String id = request.getParameter("id");// 分类id
		String obj = request.getParameter("obj");
		String pageCount = request.getParameter("pageCount");
		String city_id = request.getParameter("city_id");

		try {

			PageInfo pi = new PageInfo();
			pi.setCurrentPage(Integer.parseInt(pageCount));
			List<Map<String, Object>> datas = null;
			// 判断是否特殊类型--美食外卖的热销产品
			if (!Util.isNull(id) && id.contains("-")) {// 负数 -店铺id
				String shop_id = id.replace("-", "");
				datas = goodService.searchHotSaleGoodsByShopId(shop_id, pi);
			} else {
				// 获取缓存的子分类信息
				List<Integer> goods_type_ints = startOnLoadService.typesByParentId(id);
				String[] goods_type = startOnLoadService.int2strList(goods_type_ints);
				/*
				 * datas = goodsService.searchGoods( goods_type,
				 * Integer.valueOf(city_id), obj, pi);
				 */
			}
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "查询成功！");
			map.put("datas", datas);
			map.put("page", pi);
		} catch (Exception e) {
			logger.error(e);
			logger.error(Util.getStackTraceAsString(e));
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.write(JsonUtil.object2json(map));
		out.close();
	}

	/** -----------------商家端查询，根据类型查询商品列表--------------- ***/

	/**
	 * 根据shop_id token type pageCount 查询商品列表，做分页
	 */
	@RequestMapping("/getGoodsByShopidAndType")
	public void getGoodsByShopidAndType(HttpServletRequest request, HttpServletResponse response) throws IOException {
		Map<String, Object> map = new HashMap<String, Object>();

		try {
			String shop_id = request.getParameter("shop_id");
			String token = request.getParameter("token");
			String pageCount = request.getParameter("pageCount");
			String cCode = request.getParameter("cCode");
			Map<String, String> params = new HashMap<String, String>();
			params.put("shop_id", shop_id);
			params.put("pageCount", pageCount);
			params.put("cCode", cCode);
			if (checkService.checkToken(shop_id, token, map)) {
				goodService.shopSearchGoodsByType(params, map);
			}

		} catch (Exception e) {
			logger.error(e);
			logger.error(Util.getStackTraceAsString(e));
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.write(JsonUtil.object2json(map));
		out.close();
	}

	/**
	 * 条件查询人气爆款/热门商品列表
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/getGoodsByPromotionType")
	public void getGoodsByPromotionType(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// if(UserLogin.isUserLogin(request, response, systemService)){
		try {
			String promotion_type = request.getParameter("promotion_type");
			String pageCount = request.getParameter("pageCount");// 查看的当前页
			PageInfo page = new PageInfo();
			page.setShowCount(5);
			page.setCurrentPage(Integer.parseInt(pageCount));// 设置要读取的页
			List<ApiGoods> datas = goodService.serchGoodsByPromotionType(Integer.parseInt(promotion_type), page);
			response.setCharacterEncoding("UTF-8");
			PrintWriter out = response.getWriter();
			Map<String, Object> map = new HashMap<String, Object>();
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put("datas", datas);
			map.put("page", page);
			out.print(JsonUtil.object2json(map));
			out.close();
		} catch (Exception e) {
			logger.error(Util.getStackTraceAsString(e));
			e.printStackTrace();
		}
		// }
	}

	/**
	 * 条件查询人气爆款/热门商品列表
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@Transactional
	@RequestMapping(value = "getGoodsByShop")
	public void getGoodsByShop(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// UserLogin.isUserLogin(request, response, systemService);
		String shop_id = request.getParameter("shop_id");
		List<ApiGoods> datas = goodService.searchGoodsByShop(Integer.parseInt(shop_id));
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		map.put("datas", datas);
		out.print(JsonUtil.object2json(map));
		out.close();
	}

	/**
	 * 待评价的商品-----已完成的订单，待评价
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@Transactional
	@RequestMapping(value = "searchGoodsByShopId")
	public void searchGoodsByShopId(HttpServletRequest request, HttpServletResponse response) throws IOException {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			String shop_id = request.getParameter("shop_id");// 查询
			String pageCount = request.getParameter("pageCount");// 查看的当前页
			PageInfo page = new PageInfo();
			page.setShowCount(5);
			page.setCurrentPage(Integer.parseInt(pageCount));// 设置要读取的页
			//
			Map<String, String> params = new HashMap<String, String>();
			params.put("shop_id", shop_id);
			goodService.searchGoodsByShopId(params, map, page);

		} catch (Exception e) {
			e.printStackTrace();
			logger.error(Util.getStackTraceAsString(e));
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常！");
		}

		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.write(JsonUtil.object2json(map));
		out.close();
	}

	/**
	 * 搜索商品
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@Transactional
	@RequestMapping(value = "searchGoods")
	public void searchGoods(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String name = request.getParameter("name");
		String cityid = request.getParameter("cityid");

		List<Map<String, Object>> datas = goodService.searchGoods(name, cityid);

		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		map.put("datas", datas);
		out.print(JsonUtil.object2json(map));
		out.close();
	}

	/*
	 * 搜索商品或者店铺
	 */
	@Transactional
	@RequestMapping(value = "searchShopsOrGoods")
	public void searchShopsOrGoods(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String i = request.getParameter("obj");
		String cityid = request.getParameter("cityid");
		String name = request.getParameter("name");
		String pageCount = request.getParameter("pageCount");
		String longitude = request.getParameter("longitude");
		String latitude = request.getParameter("latitude");
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			Map<String, String> params = new HashMap<String, String>();
			params.put("city_id", cityid);
			params.put("name", name);
			params.put("pageCount", pageCount);
			params.put("longitude", longitude);
			params.put("latitude", latitude);
			System.out.println(i);
			if (i.equals("1")) {// 搜索店铺
				apiShopsService.searchShopsByShopName(params, map);
			} else if (i.equals("2")) {
				apiT_productsService.searchProductsByGoodName(params, map);
			}

		} catch (Exception e) {
			e.printStackTrace();
			logger.error(Util.getStackTraceAsString(e));
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常！");
		}

		//
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}

	/**
	 * 图文详情
	 */
	@Transactional
	@RequestMapping(value = "imageDetail")
	public void imageDetail(HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		Map<String, Object> map = new HashMap<String, Object>();
		String id = request.getParameter("id");
		System.out.println(id);
		List<Map<String, Object>> datas = goodService.imageDetail(id);
		map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		map.put("datas", datas);
		out.print(JsonUtil.object2json(map));
		out.close();
	}

	@Transactional
	@RequestMapping(value = "tuijianGoods")
	public void tuijianGoods(HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		Map<String, Object> map = new HashMap<String, Object>();
		String type = request.getParameter("type");
		String cityid = request.getParameter("cityid");
		List<ApiGoods> datas = goodService.tuijianGoods(type, cityid);
		map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		map.put("datas", datas);
		out.print(JsonUtil.object2json(map));
		out.close();
	}

	/**
	 * insert
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@Transactional
	@RequestMapping(value = "insertT_goods")
	public void insertT_goods(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String name = request.getParameter("name");
		String mini_buy = request.getParameter("mini_buy");
		String description = request.getParameter("description");
		// String image = request.getParameter("image");

		Map<String, Object> map = new HashMap<String, Object>();

		if (name != null && !"".equals(name.trim())) {
			ApiGoods t_good = new ApiGoods();
			t_good.setName(name);
			t_good.setMini_buy(Integer.valueOf(mini_buy));
			t_good.setDescription(description);

			goodService.insertT_goods(t_good);
		}

		map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		map.put(Utils.MESSAGE, "添加成功！");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}

	/**
	 * delete
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@Transactional
	@RequestMapping(value = "deleteT_goods")
	public void deleteT_goods(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String id = request.getParameter("id");

		Map<String, Object> map = new HashMap<String, Object>();

		if (id != null && !"".equals(id.trim())) {
			goodService.deleteT_goods(Integer.valueOf(id));

			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "删除成功！");
		} else {
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "检验失败！");
		}

		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}

	/**
	 * 上传文件
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@Transactional
	@RequestMapping(value = "/upload")
	public void upload(@RequestParam(value = "file", required = false) MultipartFile file, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		// ModelMap model
		// System.out.println("开始");
		String goodsid = request.getParameter("goodsid");
		// goodsid = "1";
		String path = ImageUtils.getServletWebAppsPath(request) + "/upload" + "/" + goodsid;
		String fileName = file.getOriginalFilename();
		// System.out.println(path);
		File targetFile = new File(path, fileName);
		if (!targetFile.exists()) {
			targetFile.mkdirs();
		}
		// 保存
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			file.transferTo(targetFile);
			ApiGoods_p p = new ApiGoods_p();
			p.setGoodsId(Long.valueOf(goodsid));
			p.setFileName(fileName);
			p.setPath(ImageUtils.getDataBaseImagePath() + "/upload/" + goodsid + "/" + fileName);
			p.setCommit_time(new Date());
			p.setNote(request.getParameter("note"));
			p.setStatus(0);
			goodService.insertGoods_p(p);

			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put("datas", p);
			map.put(Utils.MESSAGE, "上传成功！");

		} catch (Exception e) {
			logger.error(Util.getStackTraceAsString(e));
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常！");
		}
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}

	/**
	 * 上传缩略图
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */

	@Transactional
	@RequestMapping(value = "picUpload")
	public void picUpload(@RequestParam(value = "file", required = false) MultipartFile file,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		// ModelMap model

		String path = ImageUtils.getServletWebAppsPath(request) + "/goods/";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		String sdfStr = sdf.format(new Date());
		String fileName = sdfStr + "_" + file.getOriginalFilename();

		File targetFile = new File(path, fileName);
		if (!targetFile.exists()) {
			targetFile.mkdirs();
		}
		String smallpic_path = "";
		Map<String, Object> map = new HashMap<String, Object>();
		// 保存
		try {
			file.transferTo(targetFile);
			smallpic_path = ImageUtils.getDataBaseImagePath() + "/goods/" + fileName;

			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "上传成功！");
			map.put("datas", smallpic_path);

		} catch (Exception e) {
			logger.error(Util.getStackTraceAsString(e));
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常！");
		}
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}

	/**
	 * 新增商品信息（包括产品规格和产品图片）
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@Transactional
	@RequestMapping(value = "insertTotalGoods")
	public void insertTotalGoods(@RequestParam(value = "file", required = false) MultipartFile[] file,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		String name = request.getParameter("name");
		String mini_buy = request.getParameter("mini_buy");
		String type = request.getParameter("type");
		String city_id = request.getParameter("city_id");
		String shop_id = request.getParameter("shop_id");
		Integer shop_type = apiShopsService.searType(shop_id);
		String sort = request.getParameter("sort");
		String description = request.getParameter("description");
		String productStr = request.getParameter("productStr");
		String image = request.getParameter("image");
		String image_list = request.getParameter("image_list");

		Map<String, Object> map = new HashMap<String, Object>();

		Map<String, String> params = new HashMap<String, String>();
		params.put("name", name);
		// params.put("description", description);
		try {
			if (checkService.checkParams(params, map)) {
				ApiGoods t_good = new ApiGoods();
				t_good.setName(name);
				t_good.setType(Integer.valueOf(type));
				t_good.setMini_buy(Integer.valueOf(mini_buy));
				t_good.setDescription(description);
				t_good.setImage(image);
				t_good.setCity_id(Integer.valueOf(city_id));
				t_good.setShop_id(Integer.valueOf(shop_id));
				t_good.setPar_type(shop_type);
				t_good.setSort(Integer.valueOf(sort));
				t_good.setStatus("1");// 在售中
				//
				// Integer ret_i = goodService.insertT_goodsAll(t_good,
				// productStr,
				// image_list,null);
			}
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "添加成功！");
		} catch (Exception e) {
			logger.error(Util.getStackTraceAsString(e));
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常！");
		}
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}

	/**
	 * 更新商品信息（包括产品规格和产品图片）
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@Transactional
	@RequestMapping(value = "updateTotalGoods")
	public void updateTotalGoods(HttpServletRequest request, HttpServletResponse response) throws Exception {
		/** 这里修改图片、图片列表、名称 最小购买数、商品描述--规格 */
		String name = request.getParameter("name");// 商品名称
		String mini_buy = request.getParameter("mini_buy");// 最小购买数量
		String shop_id = request.getParameter("shop_id");// 店铺id
		String description = request.getParameter("description");
		String productStr = request.getParameter("productStr");
		if (productStr == null)
			productStr = ";";
		String image = request.getParameter("image");
		String image_list = request.getParameter("image_list");

		Map<String, Object> map = new HashMap<String, Object>();
		String id = request.getParameter("id");

		try {
			if (id != null && !"".equals(id.trim())) {

				ApiGoods t_good = goodService.findT_goodsById(Long.valueOf(id));

				t_good.setName(name);
				t_good.setShop_id(Integer.valueOf(shop_id));
				t_good.setMini_buy(Integer.valueOf(mini_buy));
				t_good.setDescription(description);
				t_good.setImage(image);
				int ret_i = goodService.updateT_goodsAll(t_good, productStr, image_list, ",", null, null);
				//
				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "更新成功！");
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(Util.getStackTraceAsString(e));
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常");
		}

		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}

	/**
	 * 新增erp产品信息（包括产品规格和产品图片）
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@Transactional
	@RequestMapping(value = "insertTotalGoods4erp")
	public void insertTotalGoods4erp(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String name = request.getParameter("name");
		String mini_buy = request.getParameter("mini_buy");
		String goods_id = request.getParameter("goods_id");
		String description = request.getParameter("description");
		String type = request.getParameter("type");
		String city_id = request.getParameter("city_id");
		String shop_id = request.getParameter("shop_id");
		String sort = request.getParameter("sort");
		String count = request.getParameter("count");
		String producing_area = request.getParameter("producing_area");
		Map<String, Object> map = new HashMap<String, Object>();

		try {
			if (name != null && !"".equals(name.trim())) {

				ApiGoods t_good = new ApiGoods();
				t_good.setId(Long.valueOf(goods_id));
				t_good.setName(name);
				t_good.setType(Integer.valueOf(type));
				t_good.setMini_buy(Integer.valueOf(mini_buy));
				t_good.setDescription(description);
				t_good.setProducing_area(producing_area);
				if (city_id == null)
					city_id = "18";
				if (count == null)
					count = "0";
				if (shop_id == null)
					shop_id = "0";
				if (sort == null)
					sort = "0";
				t_good.setCity_id(Integer.valueOf(city_id));
				t_good.setShop_id(Integer.valueOf(shop_id));
				t_good.setSort(Integer.valueOf(sort));
				t_good.setSource("erp");
				goodService.insertT_goodsAll4erp(t_good);
				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "添加成功！");
			}
		} catch (Exception e) {
			logger.error(Util.getStackTraceAsString(e));
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "添加失败！");
		}
		//
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}

	/**
	 * 修改上下架 删除状态
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@Transactional
	@RequestMapping(value = "updateT_goodsStatusOrDeleted")
	public void updateT_goodsStatusOrDeleted(HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		String id = request.getParameter("id");
		String status = request.getParameter("status");
		String is_deleted = request.getParameter("is_deleted");

		Map<String, Object> map = new HashMap<String, Object>();

		try {
			if (id != null && !"".equals(id.trim())) {
				ApiGoods t_good = new ApiGoods();
				// 获取原数据
				t_good = goodService.findT_goodsById(Long.valueOf(id));
				// 修改上下架
				int ret_i = 0;
				if (status != null && !"".equals(status.trim())) {
					t_good.setStatus(status);
					ret_i = goodService.updateT_goods(t_good);
				}
				// 修改删除状态
				if (is_deleted != null && !"".equals(is_deleted.trim())) {
					t_good.setIs_deleted(Integer.valueOf(is_deleted));
					ret_i = goodService.updateT_goods(t_good);
				}
				//

				if (ret_i > 0) {
					map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
					map.put(Utils.MESSAGE, "编辑成功！");
				} else {
					map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
					map.put(Utils.MESSAGE, "编辑失败！");
				}
			} else {
				map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
				map.put(Utils.MESSAGE, "检查失败！");
			}
		} catch (Exception e) {
			logger.error(Util.getStackTraceAsString(e));
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}

		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}

	/**
	 * 修改上下架 删除状态
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@Transactional
	@RequestMapping(value = "updateT_goodsStatusOrDeletedBatch")
	public void updateT_goodsStatusOrDeletedBatch(HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();

		try {
			String shop_id = request.getParameter("shop_id");
			String token = request.getParameter("token");

			String id_list = request.getParameter("id_list");// 传goods_id 以，分隔
			String status = request.getParameter("status");// 上下架状态 1-上架，2-下架
			String is_deleted = request.getParameter("is_deleted");// 暂且不用 1-删除
																	// 上下架时默认传0
			Map<String, String> params = new HashMap<String, String>();
			params.put("id_list", id_list);
			params.put("status", status);
			params.put("is_deleted", is_deleted);
			if (checkService.checkToken(shop_id, token, map)) {
				goodService.updateGoodsTopOrDel(params, map);
			}
		} catch (Exception e) {
			logger.error(Util.getStackTraceAsString(e));
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}

		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}

	@Transactional
	@RequestMapping(value = "goodsDetail")
	public void goodsDetail(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();

		List<ApiT_goods_p> list = new ArrayList<ApiT_goods_p>();
		String goodsid = request.getParameter("goodsid");
		list = goodspService.goodsImage(goodsid);
		ApiGoods goods = goodService.searchGoodsById(Long.parseLong(goodsid));
		map.put("imageList", list);
		map.put("goods", goods);

		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}

	@Transactional
	@RequestMapping(value = "goodsText")
	public @ResponseBody ApiGoods goodsText(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String goodsid = request.getParameter("goodsid");
		return goodService.searchGoodsById(Long.parseLong(goodsid));
	}

	@Transactional
	@RequestMapping(value = "searShopAllGoods")
	public void searShopAllGoods(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Map<String, Object> res = new HashMap<String, Object>();
		String shopid = request.getParameter("shopid");
		try {
			PageInfo page = new PageInfo();
			String pageCount = request.getParameter("pageCount");
			page.setCurrentPage(Integer.parseInt(pageCount));

			res.put("datas", goodService.searShopAllGoodsListPage(shopid, page));
			res.put("page", page);
			res.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			res.put(Utils.MESSAGE, "查询成功！");
		} catch (Exception e) {
			logger.error(Util.getStackTraceAsString(e));
			e.printStackTrace();
			res.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			res.put(Utils.MESSAGE, "系统异常！");
		}
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(res));
		out.close();
	}

	@Transactional
	@RequestMapping(value = "searByGoods")
	public void searByGoods(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String type = request.getParameter("type");
		String pageCount = request.getParameter("pageCount");
		String cityid = request.getParameter("cityId");
		String obj = request.getParameter("obj");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(goodService.fujinGoods(type, pageCount, cityid, obj)));
		out.close();
	}

	@Transactional
	@RequestMapping(value = "searByShop")
	public void searByShop(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String type = request.getParameter("type");
		String cityid = request.getParameter("cityId");
		String longitude = request.getParameter("longitude");// 经度
		String latitude = request.getParameter("latitude");// 纬度
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(goodService.fujinShop(type, cityid, longitude, latitude)));
		out.close();
	}

	/**
	 * 根据专场筛选列表
	 * 
	 * @author JW
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@Transactional
	@RequestMapping(value = "getGoodInfoByCondition")
	public void getGoodInfoByCondition(HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			String key = request.getParameter("key");
			String is_recommend = request.getParameter("is_recommend");
			List<Map<String, Object>> datas = goodService.getGoodInfoByCondition(key, is_recommend);
			map.put("datas", datas);
			map.put(Utils.MESSAGE, "查询成功");
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(Util.getStackTraceAsString(e));
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常！");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	/**
	 * 根据商品id获得商品信息
	 * 
	 * @author JW
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@Transactional
	@RequestMapping(value = "getGoodInfoByGoodId")
	public void getGoodInfoByGoodId(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			String id = request.getParameter("id");
			map = goodService.getGoodInfoByGoodId(Integer.parseInt(id), request.getParameter("userId"),
					request.getParameter("topicId"));
			map.put(Utils.MESSAGE, "查询成功");
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(Util.getStackTraceAsString(e));
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常！");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	/**
	 * 分页获得商品点评
	 * 
	 * @author JW
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "getDianPingByPage")
	public void getDianPingByPage(HttpServletRequest request, HttpServletResponse response, PageInfo pageInfo)
			throws Exception {
		Map<String, Object> map = new HashMap<>();
		try {
			String id = request.getParameter("id");
			List<Map<String, Object>> listMap = dianPingService.getDianPingPageByGoodId(Integer.parseInt(id), pageInfo);
			for (Map<String, Object> map2 : listMap) {
				map2.put("C_DTYPE", map2.get("c_dtype").toString());
			}
			map.put("datas", listMap);
			map.put(Utils.MESSAGE, "查询成功");
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(Util.getStackTraceAsString(e));
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常！");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}
}
